package com.witsight.cassandra.dao;

import java.io.Serializable;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.query.QueryLookupStrategy;


/**   
 * 说明: 自定义Cassandra仓库工厂Bean
 * @Title: CustomJpaRepositoryFactoryBean.java 
 * @Package com.witsight.cassandra.dao 
 * @See: {@link CassandraRepositoryFactoryBean}
 * Copyright: Copyright (c) 2017
 * Company:sany huax witsight team by product
 * @author: penght  
 * @date: 2017年9月26日 上午10:33:48 
 * @version: V1.0
 *
 */
public class CustomCassandraRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>  
	extends CassandraRepositoryFactoryBean<T, S, ID> {
	/**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger(CustomCassandraRepositoryFactoryBean.class);
    // CassandraTemplate 设置
    private CassandraTemplate cassandraTemplate;
    @Autowired(required=false)
    private NameQueriesRepository nameQueriesRepository;
    @Autowired
    private CassandraCommonRepository cassandraRepository;
    
	public CustomCassandraRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
		super(repositoryInterface);
	}
	
	/**
	 * 创建自己封装的工厂类。
	 * (non-Javadoc)  
	 * @Title: createRepositoryFactory
	 * @See: 
	 * @author penght 
	 * @param entityManager 实体管理器
	 * @return RepositoryFactorySupport工厂类
	 * @date: 2015年4月22日 下午12:15:33
	 */
	@Override
	protected RepositoryFactorySupport createRepositoryFactory() {
		if (log.isInfoEnabled()) {
			log.info("CustomCassandraRepositoryFactory be instanced.");
		}
		return new CustomCassandraRepositoryFactory(cassandraTemplate, cassandraRepository);
	}
	/**
	 * Configures a {@link NamedQueries} instance to be handed to the {@link QueryLookupStrategy} for query creation.
	 * 
	 * @param namedQueries the namedQueries to set
	 */
	public void setNamedQueries(NamedQueries namedQueries) {
		if (this.nameQueriesRepository == null) {
			super.setNamedQueries(namedQueries);
			return;
		}
		super.setNamedQueries(nameQueriesRepository);
	}
	@Override
	public void setCassandraTemplate(CassandraTemplate cassandraTemplate) {
		this.cassandraTemplate = cassandraTemplate;
		super.setCassandraTemplate(cassandraTemplate);
	}
}
